package com.farm.wcp.controller;

import com.farm.authority.controller.WebUserController;
import com.farm.authority.domain.User;
import com.farm.authority.service.UserServiceInter;
import com.farm.core.auth.exception.CheckCodeErrorException;
import com.farm.core.page.OperateType;
import com.farm.core.page.ViewMode;
import com.farm.wcp.util.CheckCodeUtil;
import com.farm.wcp.util.ThemesUtil;
import com.farm.web.WebUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author cai
 * <p>
 * create at 2021/3/19
 */
@RequestMapping("/regist")
@Controller
public class RegWebController extends WebUtils {

    private final static Logger log = Logger.getLogger(WebUserController.class);

    @Resource
    UserServiceInter userServiceImpl;

    /**
     * 进入站内注册页面
     *
     * @param session
     * @return
     */
    @RequestMapping("/PubReg")
    public ModelAndView pubLogin(HttpSession session, HttpServletRequest request) {
        ViewMode view = ViewMode.getInstance();
        return view.returnModelAndView(ThemesUtil.getThemePage("home-reg", request));
    }

    @RequestMapping("/reg")
    @ResponseBody
    public Map<String, Object> addSubmit(User user, HttpSession session) {
        try {
            user.setPhone(user.getLoginname());
            user.setState("1");
            user.setType("2");
            User entity = userServiceImpl.reg(user, getCurrentUser(session));
            return ViewMode.getInstance().setOperate(OperateType.ADD).returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.ADD).setError(e.getMessage(), e).returnObjMode();
        }
    }

    /**
     * 提交登录请求
     *
     * @param session
     * @return
     */
    //@RequestMapping("/websubmit")
    public ModelAndView webLoginCommit(String checkcode, String name, String password, HttpServletRequest request,
                                       HttpSession session) {
        try {
            // 是否启用登录验证码
            if (!CheckCodeUtil.isCheckCodeAble(checkcode, session)) {
                throw new CheckCodeErrorException("验证码未通过");
            }



            /*name = findUserLoginName(name, session);
            if (FarmAuthorityService.getInstance().isLegality(name, password)) {
                // 登录成功
                // 注册session
                Map<String, String> loginParas = FarmAuthorityService.loginIntoSession(session, getCurrentIp(request),
                        name, "登录页");
                sendFirstMessageToUser(loginParas,
                        FarmParameterService.getInstance().getParameter("config.sys.firstlogin.message"));
                String goUrl = getGoUrl(session, FarmParameterService.getInstance());
                loginStateHandle(true, session);
                // 本地登陆
                return ViewMode.getInstance().returnRedirectUrl(goUrl);
            } else {
                // 登录失败
                loginStateHandle(false, session);
                return ViewMode.getInstance().putAttr("loginname", name).setError("用户密码错误", null).returnModelAndView(ThemesUtil.getThemePage("home-login", request));
            }*/
            return ViewMode.getInstance().putAttr("loginname", name).setError("用户密码错误", null).returnModelAndView(ThemesUtil.getThemePage("home-login", request));
        } catch (CheckCodeErrorException e) {
            return ViewMode.getInstance().putAttr("loginname", name).setError("验证码错误", null).returnModelAndView(ThemesUtil.getThemePage("home-login", request));
        }
    }

}
